package march.firstmarch22.march01;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class Driver01 {
    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {

        //1.获取job对象
        Job job = Job.getInstance(new Configuration());

        //2.关联本Driver类
        job.setJarByClass(Driver01.class);

        //3.关联Mapper和Reducer
        job.setMapperClass(Mapper01.class);
        job.setReducerClass(Reducer01.class);

        //4.设置Map端输出KV类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);

        //5.设置程序最终输出KV类型
        job.setOutputKeyClass(NullWritable.class);
        job.setOutputValueClass(Text.class);

        //判断输出路径是否存在，若存在则进行删除（省去手动删除的时间）
        Path outPath = new Path("F:\\test\\output");
        FileSystem fileSystem = outPath.getFileSystem(job.getConfiguration());
        if(fileSystem.exists(outPath)){
            fileSystem.delete(outPath,true);
        }

        //6.设置程序输入输出路径
        FileInputFormat.setInputPaths(job,new Path("F:\\test\\march"));
        FileOutputFormat.setOutputPath(job,outPath);

        //7.提交job
        boolean b = job.waitForCompletion(true);
        System.exit(b ? 0 : 1);
    }
}
